ARC596 - Tutorial 2 - Basic Unity AR App

ARC596 - Tutorial 2 - Basic Unity AR App

Requirements

  1. Rhinoceros 7
  2. Github Desktop
  3. Anaconda
  4. Unity 2022.3.3f1
    *Note: Android SDK and Java JDK (when developing for Android) - have to be ticked in the installation modules when installing Unity.*
    

Dependencies

  1. COMPAS
  2. COMPAS Fab - Fabrication Library for Robots
  3. COMPAS Eve - Communication
  4. Vuforia
  5. ROS#

Installing the Dependencies

  1. Install the correct Unity Version using Unity Hub 2022.3.3f1 https://unity.com/releases/editor/whats-new/2022.3.3
    • Note: Do not click the blue download link. If you download directly from this website it is cubersome to install the dependencies. Unity is version-sensitive, and it needs to be exactly this version

    • Be sure to select the appropriate dependencies below.

      • Note: this installs roughly 20gb of data, make sure you have enough free space on your computer.

        • Microsoft Visual Studio
        • Android Build Support (Both Android SDK and OpenJDK)
        • iOS Build Support
        • Universal Windows Platform Build Support
        • Windows Build Support (IL2CPP)
        • Documentation
  2. Run Unity Hub. Create a new Unity Project. Be sure to select the correct Unity Version as basis for the project 2022.3.3f1

  3. Install Unity Packages for XR (Mixed Reality)

    • Access the Package Manager

    • Switch to the Unity Registry

    • Search & Install AR Foundation, repeat for ARCore XR plugin

    • Select No on whether to use the new input system package.

    • Add ARCore Unity Extensions. Select Add package from git URL, https://github.com/google-ar/arcore-unity-extensions.git

    • Verify AR Foundation, ARCore XR plugin, ARCore Extensions are installed

Unity Overview

The Scene View

A Scene contains the environments and menus of your game. Think of each unique Scene file as a unique level. In each Scene, you place your environments, obstacles, and decorations, essentially designing and building your game in pieces. Scene view is your interactive view into the world you are creating. You can use the Scene view to select and position scenery, characters, Cameras , lights, and all other types of Objects. Selecting, manipulating, and modifying GameObjects in the Scene view are some of the first skills you must learn to begin working in Unity.

More info

Scene View Navigation

The Gizmo allows you to quickly modify the viewing angle and the projection mode.

Arrow movement

You can use the Arrow Keys to move around the Scene as though walking through it. The up and down arrows move the Camera forward and backward in the direction it is facing. The left and right arrows pan the view sideways.

Basic Tools

You can use the following Buttons to select, move, rotate, scale. This toolbar is located on the top left corner.

  1. Hand Tool (Keyboard Shortcut: Q)
  2. Move Tool (Keyboard Shortcut: W)
  3. Rotate Tool (Keyboard Shortcut: E)
  4. Scale tool (Keyboard Shortcut: R)

The Game View

The Game view is rendered from the Camera(s) in your application. It represents your final, published application. You need to use one or more Cameras to control what the player sees when they are using your application. In AR, we will see a mixed environment between real and digital objects, as the camera orientation is constantly changing by our movement in space.

Play mode

Use the buttons in the Toolbar to control the Editor Play mode and see how your published application plays. An important fact is that, in Play mode, any changes you make are temporary, and are reset when you exit Play mode.

→ More info

The Hierarchy Window

The Hierarchy window displays every GameObject in a Scene, such as models, Cameras, or Prefabs. You can use the Hierarchy window to sort and group the GameObjects you use in a Scene.

The default Hierarchy window view when you open a new Unity project

The Inspector Window

The Inspector Window is being used to view and edit properties and settings for almost everything in the Unity Editor. In the Inspector Window, one can add or remove Components , which enable different features.. We will see what these are soon. → More Info

The Project Window

The Project window displays all of the files related to your Project and is the main way you can navigate and find Assets and other Project files in your application. When you start a new Project by default this window is open. However, if you cannot find it, or it is closed, you can open it via Window > General > Project or use the keyboard command Ctrl + 9 (Command + 9 on macOS).

GameObjects, Components, Prefabs

GameObjects are the fundamental objects in Unity that represent 3d objects, props and scenery. They do not accomplish much in themselves but they act as containers for Components, which implement the real functionality.

A GameObject always has a Transform component attached (to represent position and orientation) and it is not possible to remove this. The other components that give the object its functionality can be added from the editor’s Component menu or from a script. There are also many useful pre-constructed objects (primitive shapes, Cameras, etc) available on the GameObject > 3D Object menu (more info: Primitive Objects )

Components

Components implement functionalities on the GameObjects. For example, a light object is created by attaching a light component to a GameObject. Components are contained by GameObjects. Unity has many built-in components, and you can create your own by writing scripts that inherit from the MonoBehaviour class. C# is the programming language which we use to write code.

→ More Info about Scripting in Unity

A simple Cube GameObject with several Components

Prefabs

Prefabs allow you to create, configure, and store a GameObject complete with all its components, property values, and child GameObjects .The Prefab Asset acts as a template from which you can create new prefab instances in the scene.

→ More Info

Parenting

Unity uses the concept of parent-child hierarchies, or parenting, to group GameObjects. An object can contain other GameObjects that inherit its properties. You can link GameObjects together to help move, scale, or transform a collection of GameObjects. When you move the top-level object, or parent GameObject, you also move all child GameObjects. You can also create nested parent-child GameObjects. All nested objects are still descendants of the original parent GameObject, or root GameObject.Child GameObjects inherit the movement and rotation of the parent GameObject. To learn more about this, see documentation on the Transform component!

Child 1 and Child 2 are the child GameObjects of Parent. Child 3 is a child GameObject of Child 2, and a descendant GameObject of Parent.

Create the Animation Test App

Creating new GameObjects

To create a new GameObject in the Hierarchy window:

→ You can also press Ctrl+Shift+N (Windows) or Command+Shift+N (macOS) to create a new empty GameObject.

Creating child GameObjects

To create a child GameObject:

Creating parent GameObjects

You can add a new GameObject into the Hierarchy view as the parent of existing GameObjects.

To create a parent GameObject:

You can also press Ctrl+Shift+G (Windows) or Command+Shift+G (macOS) to create a parent GameObject.

→ You can also click and drag GameObjects inside, or outside parent GameObjects.

Duplicating GameObjects

To duplicate GameObjects, right-click the target GameObject and select Duplicate.

→ You can also press Ctrl+D (Windows) or Command+D (macOS) to duplicate the selected GameObject.

Task:

Try inserting multiple GameObjects like cubes, spheres and Planes in the Scene. Use the basic tools to Move, Rotate and Scale the Objects and go to the Inspector Window. Try to transform them Manually by Inserting Values on Position, Rotation and Scale.

Note: To quickly reset all the transform values, you can right click on the transform title, and click Reset

Make a new Material

Note: You can explore different Material Properties, such as Transparency (in Rendering Mode), Emission (“glowy” effect, especially when combined with Bloom Rendering effects in Gaming), or add a Texture as a Map, similarly to other softwares.

Tip: If you don’t see the camera and light icons, try to click on the Gizmos button on top.

Events in Unity

An event is a message sent to an object to signal that an event happened such as the pressing of a button has occurred. An event sender pushes notifications that an event happened and a receiver receives that notification and defines to respond to it. The object that raises the event is called the event sender. The event sender doesn’t know which object will receive the events it raises.

Event Functions

A script in Unity is not like the traditional idea of a program where the code runs continuously in a loop until it completes its task. Instead, Unity passes control to a script intermittently by calling certain functions that are declared within it. Once a function has finished executing, control is passed back to Unity. These functions are known as event functions since they are activated by Unity in response to events that occur during gameplay. Unity uses a naming scheme to identify which function to call for a particular event. For example, you will already have seen the Update function (called before a frame update occurs) and the Start function (called just before the object’s first frame update).

Note: Many more event functions are available in Unity; the full list can be found in the script reference page for the MonoBehaviour class along with details of their usage. The following are some of the most common and important events.

Create a C# script for keyboard interaction

Let’s create our first C# script to interact with the Cube we made previously.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Zoom : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

MonoBehaviour:

Unity’s base class that all scripts should derive from if they are used as components.

MonoBehaviour is a part of the UnityEngine namespace and implements a big list of event functions.

Start and Update

Unity adds the Start and Update methods by default to all new scripts.

Start: Runs once, at the first time the component is initialized.

Update: Runs on every frame (e.g. if it is 60fps, it runs 60 times per second!)

→ We will write a script in Update, but will create an Event that triggers the command only when we press a button.

Declare a variable

Note: Remember to always put a semicolon (;) at the end of every line_

public class Zoom: MonoBehaviour {
  
  public GameObject cube;
  
  // Start is called before the first frame update
  void Start() {
      
  }
  
  // Update is called once per frame
  void Update() {
      
  }
}

Add an event in an “if” statement

    void Update()
    {
        if(Input.GetKey(KeyCode.Space))
        {
            //do something here
        }
 }

Add an action

     void Update()
    {
        if(Input.GetKey(KeyCode.Space))
        {
            cube.transform.localScale += (Vector3.one*1.00001f)/100;
        }
    }

Note: We used a small value, since it will move fast.

Note: Any changes to code or scene must be made after Existing Play Mode or they will be temporary.

Download animated characters

https://assetstore.unity.com/

Note: The Unity Asset Store is home to a growing library of free and commercial assets created both by Unity Technologies and also members of the community. A wide variety of assets are available, covering everything from textures, models and animations, to whole project examples, tutorials and Extension Assets.

Find and download the free asset 5 animated Voxel animals.

https://assetstore.unity.com/packages/3d/characters/animals/5-animated-voxel-animals-145754

Import Package in your Unity File

Make a Plane

Make sure the Transform position values are set to 0,0,0

Import a character

Tip: To zoom in on an object fast, you can first select it from the Hierarchy or the scene and then click on the “F” button on your keyboard.

You can move the camera to a position where you see everything nicely.

Click on Play!

By clicking on the Keyboard arrows your character will move, by clicking on the Space Button the character will jump! If the cube gets bigger, the dog collides with it and might fall. The character is a Rigid Body with Collision properties.

Regular Update Events

A game is rather like an animation where the animation frames are generated on the fly. A key concept in games programming is that of making changes to position, state and behavior of objects in the game just before each frame is rendered. The Update function is the main place for this kind of code in Unity. Update is called before the frame is rendered and also before animations are calculated.

Below is an example of the code that would make the animated figure move:

void Update() 
{
    float distance = speed * Time.deltaTime * Input.GetAxis("Horizontal");
    transform.Translate(Vector3.right * distance);
}

→ More Information on execution order of events in unity

ARCore

ARCore is Google’s platform for building augmented reality experiences. Using different APIs. ARCore enables your phone to sense its environment, understand the world and interact with information. Some of the APIs are available across Android and iOS to enable shared AR experiences.

ARCore uses three key capabilities to integrate virtual content with the real world as seen through your phone’s camera:

Supported devices

ARCore is designed to work on a wide variety of qualified Android phones running Android 7.0 (Nougat) and later. A full list of all supported devices is available here.

How does ARCore work?

→ For a more detailed breakdown of how ARCore works, check out __fundamental concepts_._

Android // Android NDK // Unity (AR Foundation) // iOS // Unreal

Note: In our class/tutorial, we will focus on Unity’s AR Foundation Framework, and build the application for Android devices.

AR Foundation

AR Foundation is a cross-platform framework that allows you to build augmented reality experiences once, then build for either Android or iOS devices. ARCore Extensions for AR

→ More information about AR Foundation